package com.example.phone.search;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.example.phone.R;
import com.example.phone.base.BaseActivity;
import com.example.phone.base.RequestInterface;
import com.example.phone.base.ServerResponse;
import com.example.phone.bean.MyPageInfo;
import com.example.phone.bean.SearchResult;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnMultiPurposeListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class SearchDetailActivity extends BaseActivity {

    @BindView(R.id.detail_view)
    RecyclerView detailView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private SearchResultAdapter searchResultAdapter;
    private List<SearchResult> searchResults = new ArrayList<>();
    private String keyword;
    private int pageNum = 1;
    private Handler mHandler = new Handler();


    @Override
    public void initData() {
        keyword = getIntent().getStringExtra("keyword");
        getSearchResult(keyword);
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);

        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageNum = 1;
                getSearchResult(keyword);
                refreshLayout.finishRefresh(2000);
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNum++;
                getSearchResult(keyword);
                refreshLayout.finishLoadMore(150);
            }
        });

        searchResultAdapter = new SearchResultAdapter(getContext(), searchResults);

        detailView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        detailView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        detailView.setAdapter(searchResultAdapter);

    }

    @Override
    public int initLayout() {
        return R.layout.activity_searchdetail;
    }

    private void getSearchResult(String keyword) {
        log("send Request");
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(RequestInterface.basePath)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        final RequestInterface.SearchRequest searchRequest = retrofit.create(RequestInterface.SearchRequest.class);
        Call<ServerResponse<MyPageInfo<SearchResult>>> call = searchRequest.getProductList(keyword, pageNum, 10);
        call.enqueue(new Callback<ServerResponse<MyPageInfo<SearchResult>>>() {
            @Override
            public void onResponse(Call<ServerResponse<MyPageInfo<SearchResult>>> call, Response<ServerResponse<MyPageInfo<SearchResult>>> response) {
                if (response.body().getStatus() == 0) {
                    MyPageInfo<SearchResult> pageInfo = response.body().getData();

                    if (pageInfo != null) {
                        List<SearchResult> list = pageInfo.getList();
                        if (list != null && list.size() > 0) {
                            if (pageNum == 1) {
                                searchResults.clear();
                            }
                            searchResults.addAll(list);
                            searchResultAdapter.notifyDataSetChanged();
                        } else {
                            toastShort("已经到底了");
                        }
                    } else {
                        toastShort("抱歉，没有找到相应的产品");
                    }
                } else {
                    toastShort("搜索失败，请检查网络");
                }
            }

            @Override
            public void onFailure(Call<ServerResponse<MyPageInfo<SearchResult>>> call, Throwable t) {
                toastShort("加载失败，请检查网络");
            }
        });
    }



}
